package com.sohh.demo.demo0522;

import org.junit.Test;

import java.util.*;

/**
 * @description:
 * @author: ruoan
 * @date: 2021/5/16 15:57
 */
public class Test15 {

    final List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 5000.00),
            new Employee("李四", 38, 3000.00),
            new Employee("王五", 48, 7000.20),
            new Employee("赵六", 28, 326.25)
    );
    @Test
    public void test1(){
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };
        // 通过传入比较器比较元素
        TreeSet<Integer> treeSet = new TreeSet<>(comparator);
        treeSet.add(11);
        treeSet.add(3);
        treeSet.add(51);
        treeSet.add(42);
        treeSet.add(8);

        System.out.println(treeSet);

    }


    public List<Employee> getEmployeesBySalary(List<Employee> employees){
        List<Employee> emps = new ArrayList<>();
        for (Employee e : employees) {
            if (e.getSalary() >= 3000) {
                emps.add(e);
            }
        }
        return emps;
    }

    public List<Employee> getEmployeesByAge(List<Employee> employees){
        List<Employee> emps = new ArrayList<>();
        for (Employee e : employees) {
            if (e.getAge() >= 30) {
                emps.add(e);
            }
        }
        return emps;
    }


    @Test
    public void test2(){

        final List<Employee> employeesBySalary =
                getEmployeesBySalary(employees);
        System.out.println(employeesBySalary);

        final List<Employee> employeesByAge =
                getEmployeesByAge(employees);
        System.out.println(employeesByAge);

    }



    public List<Employee> filterEmployee(List<Employee> employees,MyPredicate<Employee> predicate){
        List<Employee> employeeList = new ArrayList<>();
        for (Employee e : employees) {
            if (predicate.test(e)) {
                employeeList.add(e);
            }
        }
        return employeeList;
    }


    /**
     * 通过策略模式改进代码
     */
    @Test
    public void test3(){
        final List<Employee> employeeList = filterEmployee(employees, new FilterEmployeeByAge());
        System.out.println(employeeList);
    }

    /**
     * 通过策略模式改进代码(匿名内部类精简代码）
     */
    @Test
    public void test4(){
        final List<Employee> employeeList = filterEmployee(employees, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getSalary()>3000;
            }
        });
        System.out.println(employeeList);
    }



    /**
     * 通过策略模式改进代码(匿名内部类精简代码）（采用lambda继续精简）
     */
    @Test
    public void test5(){
        final List<Employee> employeeList = filterEmployee(employees, e->e.getSalary()>3000);
        employeeList.forEach(System.out::println);
    }
}
